package com.jxw.launcher;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.jxw.launcher.AppInfo;
import com.jxw.launcher.BackListener;
import com.jxw.launcher.DragGrid;
import com.jxw.launcher.db.PasswordSetHelper;
import com.jxw.launcher.util.Logger;
import com.jxw.launcher.util.UserData;
import com.jxw.launcher.view.CommonDialog;
import com.jxw.studyrecord.StudyRecordMng;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/* loaded from: classes.dex */
public class MyFragmentZH extends BaseFragment {
    private static final String TAG = "MyFragment7";
    AppInfo addAppinfo;
    private DragGrid freqListView;
    private Context mContext;
    private Handler mHandler;
    private DragAdapter mVpAdapter;
    private List<AppInfo> mlistAppInfo = null;
    BackListener.BackSureListener backSureListener = new BackListener.BackSureListener() { // from class: com.jxw.launcher.MyFragmentZH.11
        @Override // com.jxw.launcher.BackListener.BackSureListener
        public void backSuccess(boolean z) {
            Log.i(MyFragmentZH.TAG, "backSuccess: ");
            Common.isDrag = false;
            MyFragmentZH.this.freqListView.refresh();
            MyFragmentZH.this.mVpAdapter.setisDelete(false);
            MyFragmentZH.this.mVpAdapter.notifyDataSetChanged();
        }
    };

    @Override // android.support.v4.app.Fragment
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View inflater = inflater(layoutInflater, viewGroup, R.layout.fg_content_zh);
        this.mContext = getActivity();
        initView1(inflater);
        BackListener.addBackListener(this.backSureListener);
        this.mHandler = new Handler();
        this.addAppinfo = new AppInfo.Builder().appLabel("添加").appIcon(this.mContext.getResources().getDrawable(R.drawable.zh_tj)).build();
        return inflater;
    }

    private void startActivitySafely2(final String str) {
        final CommonDialog commonDialog = new CommonDialog(this.mContext);
        commonDialog.setTitle(R.string.code_title);
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.layout_input_edit, (ViewGroup) null);
        commonDialog.setContent(inflate);
        final EditText editText = (EditText) inflate.findViewById(R.id.edit_user);
        editText.setHint(this.mContext.getString(R.string.input_code_hint));
        commonDialog.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() { // from class: com.jxw.launcher.MyFragmentZH.1
            @Override // android.content.DialogInterface.OnClickListener
            public void onClick(DialogInterface dialogInterface, int i) {
                String parentPassword = UserData.getParentPassword();
                String masterPassword = UserData.getMasterPassword();
                String obj = editText.getText().toString();
                Log.d("设置", "" + obj);
                if (TextUtils.isEmpty(obj)) {
                    Toast.makeText(MyFragmentZH.this.mContext, (int) R.string.input_code_hint, 0).show();
                } else if (!obj.equals(parentPassword) && !obj.equals(masterPassword)) {
                    Toast.makeText(MyFragmentZH.this.mContext, (int) R.string.input_code_wrong, 0).show();
                } else {
                    MyFragmentZH myFragmentZH = MyFragmentZH.this;
                    myFragmentZH.startApkByPackName1(myFragmentZH.mContext, str);
                    commonDialog.dismiss();
                }
            }
        });
        commonDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { // from class: com.jxw.launcher.MyFragmentZH.2
            @Override // android.content.DialogInterface.OnClickListener
            public void onClick(DialogInterface dialogInterface, int i) {
                commonDialog.cancel();
            }
        });
        commonDialog.show();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void startApkByPackName1(Context context, String str) {
        try {
            Intent launchIntentForPackage = context.getPackageManager().getLaunchIntentForPackage(str);
            if (launchIntentForPackage == null) {
                launchIntentForPackage = context.getPackageManager().getLeanbackLaunchIntentForPackage(str);
            }
            if (launchIntentForPackage == null) {
                launchIntentForPackage = new Intent();
                launchIntentForPackage.setClassName(str, "com.android.launcher3.ESLauncher");
            }
            context.startActivity(launchIntentForPackage);
            StudyRecordMng.startCount(context, str, "", "", true);
        } catch (Exception e) {
            Log.d("zzj", "startApkByPackName error : " + e.toString());
            e.printStackTrace();
            Intent intent = new Intent();
            intent.setClassName(str, "com.android.launcher3.uioverrides.QuickstepLauncher");
            try {
                context.startActivity(intent);
            } catch (Exception e2) {
                Log.d("zzj", "startApkByPackName error : " + e2.toString());
                e2.printStackTrace();
                Intent intent2 = new Intent();
                intent2.setClassName(str, "com.android.launcher3.Launcher");
                try {
                    context.startActivity(intent2);
                } catch (Exception e3) {
                    Log.d("zzj", "startApkByPackName error : " + e3.toString());
                    e3.printStackTrace();
                }
            }
        }
    }

    private void initView1(View view) {
        this.freqListView = (DragGrid) view.findViewById(R.id.gridView_id);
        this.mlistAppInfo = new ArrayList();
        this.mVpAdapter = new DragAdapter(this.mContext, this.mlistAppInfo, this.freqListView);
        this.freqListView.setRelativeLayout((RelativeLayout) view.findViewById(R.id.ll));
        this.freqListView.setAdapter((ListAdapter) this.mVpAdapter);
        this.mVpAdapter.setToDelete(false);
        this.freqListView.setOnTouchInvalidPositionListener(new DragGrid.OnTouchInvalidPositionListener() { // from class: com.jxw.launcher.MyFragmentZH.3
            @Override // com.jxw.launcher.DragGrid.OnTouchInvalidPositionListener
            public boolean onTouchInvalidPosition(int i) {
                if (MyFragmentZH.this.mVpAdapter != null && MyFragmentZH.this.mVpAdapter.isDelete()) {
                    MyFragmentZH.this.mVpAdapter.setisDelete(false);
                    MyFragmentZH.this.mVpAdapter.notifyDataSetChanged();
                }
                return false;
            }
        });
        ((RelativeLayout) view.findViewById(R.id.rela_root)).setOnClickListener(new View.OnClickListener() { // from class: com.jxw.launcher.MyFragmentZH.4
            @Override // android.view.View.OnClickListener
            public void onClick(View view2) {
                MyFragmentZH.this.mVpAdapter.setisDelete(false);
                MyFragmentZH.this.mVpAdapter.notifyDataSetChanged();
            }
        });
        this.freqListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { // from class: com.jxw.launcher.MyFragmentZH.5
            @Override // android.widget.AdapterView.OnItemClickListener
            public void onItemClick(AdapterView<?> adapterView, View view2, int i, long j) {
                Log.i(MyFragmentZH.TAG, "onItemCldick: 8989");
                AppInfo appInfo = (AppInfo) MyFragmentZH.this.mlistAppInfo.get(i);
                if (appInfo != null) {
                    if (!TextUtils.isEmpty(appInfo.getPkgName())) {
                        Logger.getLogger().e("onItemClick 11");
                        MyFragmentZH myFragmentZH = MyFragmentZH.this;
                        myFragmentZH.startApkByPackName(myFragmentZH.mContext, appInfo.getPkgName());
                    } else if (!TextUtils.isEmpty(appInfo.mStargss)) {
                        Logger logger = Logger.getLogger();
                        logger.e("onItemClick66 " + appInfo.mStargss);
                        MyApp.getInstance().mBottomBtnOnClickListener.setContext(MyFragmentZH.this.getActivity());
                        MyApp.getInstance().mBottomBtnOnClickListener.onClick(appInfo.mStargss);
                    } else {
                        boolean isChecked = PasswordSetHelper.isChecked(MyFragmentZH.this.mContext.getContentResolver(), 1);
                        if (!TextUtils.isEmpty(UserData.getParentPassword()) && isChecked) {
                            Logger.getLogger().e("onItemClick 33");
                            MyFragmentZH.this.startActivitySafely();
                            return;
                        }
                        Logger.getLogger().e("onItemClick 44");
                        MyFragmentZH myFragmentZH2 = MyFragmentZH.this;
                        myFragmentZH2.startActivityForResult(new Intent(myFragmentZH2.mContext, APPActivity.class), 4);
                    }
                }
            }
        });
    }

    public void startActivitySafely1(final String str) {
        final CommonDialog commonDialog = new CommonDialog(this.mContext);
        commonDialog.setTitle(R.string.code_title);
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.layout_input_edit, (ViewGroup) null);
        commonDialog.setContent(inflate);
        final EditText editText = (EditText) inflate.findViewById(R.id.edit_user);
        editText.setHint(this.mContext.getString(R.string.input_code_hint));
        commonDialog.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() { // from class: com.jxw.launcher.MyFragmentZH.6
            @Override // android.content.DialogInterface.OnClickListener
            public void onClick(DialogInterface dialogInterface, int i) {
                String parentPassword = UserData.getParentPassword();
                String masterPassword = UserData.getMasterPassword();
                String obj = editText.getText().toString();
                if (TextUtils.isEmpty(obj)) {
                    Toast makeText = Toast.makeText(MyFragmentZH.this.mContext, (int) R.string.input_code_hint, 0);
                    TextView textView = (TextView) ((LinearLayout) makeText.getView()).getChildAt(0);
                    textView.setTextSize(MyFragmentZH.this.getResources().getDimension(R.dimen.x25));
                    textView.setTextColor(MyFragmentZH.this.getResources().getColor(R.color.black));
                    makeText.show();
                } else if (!obj.equals(parentPassword) && !obj.equals(masterPassword)) {
                    Toast makeText2 = Toast.makeText(MyFragmentZH.this.mContext, (int) R.string.input_code_wrong, 0);
                    TextView textView2 = (TextView) ((LinearLayout) makeText2.getView()).getChildAt(0);
                    textView2.setTextSize(MyFragmentZH.this.getResources().getDimension(R.dimen.x25));
                    textView2.setTextColor(MyFragmentZH.this.getResources().getColor(R.color.black));
                    makeText2.show();
                } else {
                    MyApp.getInstance().mBottomBtnOnClickListener.setContext(MyFragmentZH.this.getActivity());
                    MyApp.getInstance().mBottomBtnOnClickListener.onClick(str);
                    commonDialog.dismiss();
                }
            }
        });
        commonDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { // from class: com.jxw.launcher.MyFragmentZH.7
            @Override // android.content.DialogInterface.OnClickListener
            public void onClick(DialogInterface dialogInterface, int i) {
                commonDialog.cancel();
            }
        });
        commonDialog.show();
    }

    @Override // android.support.v4.app.Fragment
    public void onResume() {
        List<AppInfo> list = this.mlistAppInfo;
        if (list != null) {
            list.clear();
        }
        Log.i("c", "22222");
        initAppData();
        this.mVpAdapter.notifyDataSetChanged();
        super.onResume();
    }

    @Override // android.support.v4.app.Fragment
    public void onDestroy() {
        super.onDestroy();
        BackListener.removeBackListener(this.backSureListener);
    }

    public void startActivitySafely() {
        final CommonDialog commonDialog = new CommonDialog(this.mContext);
        commonDialog.setTitle(R.string.code_title);
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.layout_input_edit, (ViewGroup) null);
        commonDialog.setContent(inflate);
        final EditText editText = (EditText) inflate.findViewById(R.id.edit_user);
        editText.setHint(this.mContext.getString(R.string.input_code_hint));
        commonDialog.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() { // from class: com.jxw.launcher.MyFragmentZH.8
            @Override // android.content.DialogInterface.OnClickListener
            public void onClick(DialogInterface dialogInterface, int i) {
                String parentPassword = UserData.getParentPassword();
                String masterPassword = UserData.getMasterPassword();
                String obj = editText.getText().toString();
                if (TextUtils.isEmpty(obj)) {
                    Toast makeText = Toast.makeText(MyFragmentZH.this.mContext, (int) R.string.input_code_hint, 0);
                    TextView textView = (TextView) ((LinearLayout) makeText.getView()).getChildAt(0);
                    textView.setTextSize(MyFragmentZH.this.getResources().getDimension(R.dimen.x25));
                    textView.setTextColor(MyFragmentZH.this.getResources().getColor(R.color.black));
                    makeText.show();
                } else if (!obj.equals(parentPassword) && !obj.equals(masterPassword)) {
                    Toast makeText2 = Toast.makeText(MyFragmentZH.this.mContext, (int) R.string.input_code_wrong, 0);
                    TextView textView2 = (TextView) ((LinearLayout) makeText2.getView()).getChildAt(0);
                    textView2.setTextSize(MyFragmentZH.this.getResources().getDimension(R.dimen.x25));
                    textView2.setTextColor(MyFragmentZH.this.getResources().getColor(R.color.black));
                    makeText2.show();
                } else {
                    MyFragmentZH myFragmentZH = MyFragmentZH.this;
                    myFragmentZH.startActivityForResult(new Intent(myFragmentZH.mContext, APPActivity.class), 4);
                    commonDialog.dismiss();
                }
            }
        });
        commonDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { // from class: com.jxw.launcher.MyFragmentZH.9
            @Override // android.content.DialogInterface.OnClickListener
            public void onClick(DialogInterface dialogInterface, int i) {
                commonDialog.cancel();
            }
        });
        commonDialog.show();
    }

    private void initAppData() {
        AppInfo build = new AppInfo.Builder().appLabel("安卓平台").appStargs(getResources().getString(R.string.tag_args_launcher32)).appIcon(this.mContext.getResources().getDrawable(R.drawable.zh_azpt)).build();
        AppInfo build2 = new AppInfo.Builder().appLabel("检查更新").appStargs(getResources().getString(R.string.tag_args_updae)).appIcon(this.mContext.getResources().getDrawable(R.drawable.icon_jcgx)).build();
        AppInfo build3 = new AppInfo.Builder().appLabel("学情管理").appStargs(getResources().getString(R.string.tag_args_xqgl)).appIcon(this.mContext.getResources().getDrawable(R.drawable.mdpi_zh_xqgl)).build();
        new AppInfo.Builder().appLabel("作业帮").appStargs(getResources().getString(R.string.tag_args_zyb)).appIcon(this.mContext.getResources().getDrawable(R.drawable.mdpi_zh_zyb)).build();
        new AppInfo.Builder().appLabel("作业批改").appStargs(getResources().getString(R.string.tag_args_xyks)).appIcon(this.mContext.getResources().getDrawable(R.drawable.mdpi_icon_zypg)).build();
        this.mlistAppInfo.add(build3);
        this.mlistAppInfo.add(build);
        this.mlistAppInfo.add(build2);
        this.mVpAdapter.len = this.mlistAppInfo.size();
        getAppData();
        this.mlistAppInfo.add(this.addAppinfo);
        this.mHandler.post(new Runnable() { // from class: com.jxw.launcher.MyFragmentZH.10
            @Override // java.lang.Runnable
            public void run() {
                MyFragmentZH.this.mVpAdapter.setData(MyFragmentZH.this.mlistAppInfo);
                MyFragmentZH.this.mVpAdapter.notifyDataSetChanged();
            }
        });
    }

    private void getAppData() {
        File fileStreamPath = this.mContext.getFileStreamPath("app_list");
        if (!fileStreamPath.exists()) {
            return;
        }
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(fileStreamPath));
            PackageManager packageManager = this.mContext.getPackageManager();
            while (true) {
                String readLine = bufferedReader.readLine();
                if (readLine != null) {
                    try {
                        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(readLine, 128);
                        this.mlistAppInfo.add(new AppInfo.Builder().appLabel((String) packageManager.getApplicationLabel(applicationInfo)).pkgName(readLine).appIcon(packageManager.getApplicationIcon(applicationInfo)).build());
                    } catch (PackageManager.NameNotFoundException e) {
                        e.printStackTrace();
                    }
                } else {
                    bufferedReader.close();
                    return;
                }
            }
        } catch (FileNotFoundException e2) {
            e2.printStackTrace();
        } catch (IOException e3) {
            e3.printStackTrace();
        }
    }

    void startApkByPackName(Context context, String str) {
        try {
            context.startActivity(context.getPackageManager().getLaunchIntentForPackage(str));
            StudyRecordMng.startCount(context, str, "", "", true);
        } catch (Exception e) {
            Log.d("zzj", "startApkByPackName error : " + e.toString());
            Toast.makeText(this.mContext, "此应用已经被家长管控", 0).show();
        }
    }

    void writeAppDate(String str) {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(this.mContext.getFileStreamPath("app_list"), true);
            fileOutputStream.write((str + "\n").getBytes());
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e2) {
            e2.printStackTrace();
        }
    }

    @Override // android.support.v4.app.Fragment
    public void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        Logger logger = Logger.getLogger();
        logger.e("resultCode=" + i2 + ",requestCode=" + i);
        if (i2 == -1 && i == 4) {
            String stringExtra = intent.getStringExtra("pkg");
            for (AppInfo appInfo : this.mlistAppInfo) {
                if (!TextUtils.isEmpty(appInfo.getPkgName()) && appInfo.getPkgName().equals(stringExtra)) {
                    Toast.makeText(getContext(), "不能重复添加！", 0).show();
                    return;
                }
            }
            PackageManager packageManager = this.mContext.getPackageManager();
            try {
                ApplicationInfo applicationInfo = packageManager.getApplicationInfo(stringExtra, 128);
                AppInfo build = new AppInfo.Builder().appLabel((String) packageManager.getApplicationLabel(applicationInfo)).pkgName(stringExtra).appIcon(packageManager.getApplicationIcon(applicationInfo)).build();
                this.mlistAppInfo.remove(this.addAppinfo);
                this.mlistAppInfo.add(build);
                this.mlistAppInfo.add(this.addAppinfo);
                this.mVpAdapter.setisDelete(false);
                this.mVpAdapter.setData(this.mlistAppInfo);
                this.mVpAdapter.notifyDataSetChanged();
                writeAppDate(stringExtra);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    public boolean onKeyDown(int i, KeyEvent keyEvent) {
        Log.i("3333333", "55555555");
        Logger logger = Logger.getLogger();
        logger.e("onKeyDown" + this.mVpAdapter + ",action=" + keyEvent.getAction());
        if (i == 4 && keyEvent.getAction() == 0) {
            if (this.mVpAdapter != null) {
                Common.isDrag = false;
                this.freqListView.refresh();
                this.mVpAdapter.setisDelete(false);
                this.mVpAdapter.notifyDataSetChanged();
                return true;
            }
            return true;
        }
        return false;
    }
}
